//
//  Status.swift
//  weibosina
//
//  Created by hwm on 16/9/5.
//  Copyright © 2016年 hwm. All rights reserved.
//

import UIKit
import SDWebImage

class Status: NSObject {
    
    /// 微博创建时间
    var created_at: String? {
        didSet {
            // 1.将字符串转换为时间
            let craetDate = NSDate.dateWithStr(created_at!)
            created_at = craetDate.descDate
        }
    }
    /// 微博ID
    var id: Int = 0
    /// 微博信息内容
    var text: String?
    /// 微博来源
    var source: String?
        {
        didSet{
            // 1.截取字符串
            if let str = source
            {
                if str == ""
                {
                    return
                }
                
                // 1.1获取开始截取的位置
                let startLocation = (str as NSString).rangeOfString(">").location + 1
                // 1.2获取截取的长度
                let length = (str as NSString).rangeOfString("<", options: NSStringCompareOptions.BackwardsSearch).location - startLocation
                // 1.3截取字符串
                source = "来自:" + (str as NSString).substringWithRange(NSMakeRange(startLocation, length))
            }
        }
    }
    /// 配图数组
    var pic_urls: [[String: AnyObject]]? {
        didSet {
            // 1.初始化数组
            storedPicURLs = [NSURL]()
            // 2遍历取出所有的图片路径字符串
            for dict in pic_urls! {
                if let urlStr = dict["thumbnail_pic"] {
                    // 将字符串转换为URL保存到数组中
                    storedPicURLs?.append(NSURL.init(string: urlStr as! String)!)
                }
            }
            
        }
    }

    // 保存当前微博所有配图的URL
    var storedPicURLs: [NSURL]?
    // 用户信息
    var user: User?
    
    // 加载微博数据
    class func loadStatuses(finished: (models:[Status]?, error:NSError?)->()){
        let path = "2/statuses/home_timeline.json"
        let params = ["access_token": UserAccount.loadAccount()!.access_token!]
        
        HttpTools.shareHttpTools().GET(path, parameters: params, success: { (_, JSON) -> Void in
            //            print(JSON)
            // 1.取出statuses key对应的数组 (存储的都是字典)
            // 2.遍历数组, 将字典转换为模型
            let models = dict2Model(JSON!["statuses"] as! [[String: AnyObject]])
            //            print(models)
            
            // 缓存配图
            cacheStatusImages(models, finish: finished)
            
            // 2.通过闭包将数据传递给调用者
            finished(models: models, error: nil)
            
        }) { (_, error) -> Void in
            print(error)
            finished(models: nil, error: error)
            
        }
    }
    
    // 下载配图
    class func cacheStatusImages(list:[Status], finish:(model: [Status]?, error: NSError?)->()) {
        // 1.创建一个组
        let group = dispatch_group_create()
        
        // 1.缓存图片
        for status in list {
            
            // swift新特性, 如果条件为nil, 那么就会执行else后面的语句
            guard status.storedPicURLs != nil else {
                continue
            }
            
            
            for url in status.storedPicURLs! {
                // 将当前的下载操作添加到组中
                dispatch_group_enter(group)
                // 缓存图片
                SDWebImageManager.sharedManager().downloadImageWithURL(url, options: SDWebImageOptions.init(rawValue: 0), progress: nil, completed: { (_, _, _, _, _) in
                    
                    // 离开当前组
                    dispatch_group_leave(group)
                    print(url)
                })
            }
        }
        
        // 2.当所有图片都下载完毕再通过闭包通知调用者
        dispatch_group_notify(group, dispatch_get_main_queue()) { 
            // 能够来到这个地方, 一定是所有图片都下载完毕
            finish(model: list, error: nil)
        }
    }
    
    /// 将字典数组转换为模型数组
    class func dict2Model(list: [[String: AnyObject]]) -> [Status] {
        var models = [Status]()
        for dict in list
        {
            models.append(Status(dict: dict))
        }
        return models
    }
    
    // 字典转模型
    init(dict: [String: AnyObject])
    {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    // 执行setValuesForKeysWithDictionary, 回来到这里来设置
    override func setValue(value: AnyObject?, forKey key: String) {
        // 1. 判断当前是否正在给微博字典中的user字典赋值
        if key == "user" {
            // 2.根据user key对应的字典创建一个user模型
            user = User.init(dict: value as! [String : AnyObject])
            return
        }
        // 3,调用父类方法, 按照系统默认处理
        super.setValue(value, forKey: key)
    }
    
    // 找不到key
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    
    // 打印当前模型
    var properties = ["created_at", "id", "text", "source", "pic_urls"]
    override var description: String {
        let dict = dictionaryWithValuesForKeys(properties)
        return "\(dict)"
    }

    
}
